package Demo06;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.ColorChooserUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.URL;
import java.sql.SQLOutput;
import java.util.Objects;

/*
文本编辑器
 */
public class NoteEdit  extends JFrame  implements ActionListener {
    //面板；
    private final JPanel mainPanel;
    //菜单条
    private final JMenuBar menuBar;
    //菜单项--文本、编辑、帮助菜单，------放入菜单条
    private final JMenu menuFile;
    private final JMenu menuEdit;
    private final JMenu menuHelp;
    //项---文件、编辑……，下拉菜单
    private final JMenuItem newItem;
    private final JMenuItem openItem;
    private final JMenuItem saveItem;
    private final JMenuItem exitItem;
    private final JMenuItem copyItem;
    private final JMenuItem cutItem;
    private final JMenuItem pastItem;
    private final JMenuItem fontItem;
    private final JMenuItem coloItem;
    private final JMenuItem aboutItem;
    //弹出菜单

    //工具条定义；工具按钮……
    private final JToolBar toolBar;//属性容器
    //工具按钮
    private final JButton btnNew;
    private final JButton btnOpen;
    private final JButton btnSave;
    private final JButton btnCut;
    private final JButton btnCopy;
    private final JButton btnPaste;
    private final JButton btnFont;
    private final JButton btnColor;
    //文本框
    private final JTextArea textArea;
    //带滚动条
    private final JScrollPane scrollPane;

    private  String fileName="新文件";

    private  static int count=0;

    //构造方法；
    public  NoteEdit(){
        //实例化控件
        mainPanel=new JPanel(new BorderLayout());//边界布局
        //边界布局----实用

//        mainPanel= new JPanel(); //----流布局；
        menuBar=new JMenuBar();

        menuFile=new JMenu("文件");
        menuEdit=new JMenu("编辑");
        menuHelp=new JMenu("帮助");

        newItem=new JMenuItem("新建");
        openItem=new JMenuItem("打开");
        saveItem=new JMenuItem("保存");
        exitItem=new JMenuItem("退出");
        cutItem=new JMenuItem("剪切");
        copyItem=new JMenuItem("复制");
        pastItem=new JMenuItem("粘贴");
        fontItem=new JMenuItem("字体");
        coloItem=new JMenuItem("颜色");
        aboutItem=new JMenuItem("关于");


        toolBar=new JToolBar();
        btnColor=new JButton("颜色");
        btnCopy=new JButton("复制");
        btnCut=new JButton("剪切");
        btnFont=new JButton("字体");
        btnOpen=new JButton("打开");
        btnPaste=new JButton("粘贴");
        btnSave=new JButton("保存");
        btnNew=new JButton("新建");

        //图片引用；---按钮改变成图标；

        /*
        ImageIcon newIcon;
        newIcon = new ImageIcon(Objects.requireNonNull(NoteEdit.class.getClassLoader().getResource("http://pics.sc.chinaz.com/Files/pic/icons128/7165/g1.png")));
        btnNew=new JButton(newIcon);
        ImageIcon openIcon=new ImageIcon(Objects.requireNonNull(NoteEdit.class.getClassLoader().getResource("http://pics.sc.chinaz.com/Files/pic/icons128/7165/g1.png")));
        btnOpen=new JButton(openIcon);

         */




        //文本框
        textArea=new JTextArea();
        //滚动面板
        scrollPane=new JScrollPane(textArea);

        //将控件放入容器
        //设置主面板
        setContentPane(mainPanel);

        menuFile.add(newItem);
        menuFile.add(openItem);
        //添加一条线；
        menuFile.addSeparator();
        menuFile.add(saveItem);
        menuFile.addSeparator();
        menuFile.add(exitItem);

        menuEdit.add(cutItem);
        menuEdit.add(copyItem);
        menuEdit.add(pastItem);
        menuEdit.addSeparator();
        menuEdit.add(fontItem);
        menuEdit.add(coloItem);

        menuHelp.add(aboutItem);

        menuBar.add(menuFile);
        menuBar.add(menuEdit);
        menuBar.add(menuHelp);

        setJMenuBar(menuBar);


        //工具条
        toolBar.add(btnNew);
        toolBar.add(btnOpen);
        toolBar.add(btnSave);
        toolBar.addSeparator();
        toolBar.add(btnCut);
        toolBar.add(btnCopy);
        toolBar.add(btnPaste);
        toolBar.addSeparator();
        toolBar.add(btnFont);
        toolBar.add(btnCopy);

        //工具条放入窗口
        mainPanel.add(scrollPane,"Center");
        mainPanel.add(toolBar,"North");

        //监听
        newItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        exitItem.addActionListener(this);
        cutItem.addActionListener(this);
        copyItem.addActionListener(this);
        pastItem.addActionListener(this);
        fontItem.addActionListener(this);
        coloItem.addActionListener(this);
        aboutItem.addActionListener(this);
        btnOpen.addActionListener(this);
        btnSave.addActionListener(this);
        btnNew.addActionListener(this);
        btnCut.addActionListener(this);
        btnCopy.addActionListener(this);
        btnPaste.addActionListener(this);
        btnFont.addActionListener(this);
        btnColor.addActionListener(this);
//        btnCopy.addActionListener(this);
//        btnCopy.addActionListener(this);

        //窗口属性
        setTitle(fileName);

        setBounds(100,100,500,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
//        setResizable(false);

    }

    public static void main(String[] args) {
        new NoteEdit();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //退出
        if (e.getSource().equals(exitItem)){
            System.exit(0);
        }
        //新建
        if (e.getSource().equals(newItem)||e.getSource().equals(btnNew)){
            textArea.setText("");
            count++;
            setTitle(fileName+count);
        }
//        剪切
        if (e.getSource().equals(cutItem)||e.getSource().equals(btnCut)){
            textArea.cut();
        }
//        复制
        if (e.getSource().equals(copyItem)||e.getSource().equals(btnCopy)){
            textArea.copy();
        }
        //粘贴
        if (e.getSource().equals(pastItem)||e.getSource().equals(btnPaste)){
            textArea.paste();
        }
        //颜色
//        ColorChooserUI//颜色对话框
        if (e.getSource().equals(coloItem)||e.getSource().equals(btnColor)){
            //对颜色设置；
            //颜色方法
            textArea.setForeground(getColor());

        }
        //字体
        if(e.getSource().equals(fontItem)||e.getSource().equals(btnFont)){
            textArea.setFont(getMyFont());
        }
        
        //保存
        if(e.getSource().equals(saveItem)||e.getSource().equals(btnSave)){
            try {
                save();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

        }
        //打开
        if (e.getSource().equals(openItem)||e.getSource().equals(btnNew)){
            try {
                open();//新建打开方法
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

        }

    }

    //字体方法
    private Font getMyFont() {
        MyFont myFont =new MyFont(this);
        myFont.setVisible(true);

        return  null;
    }

    //颜色方法
    private Color getColor() {
        //颜色对话框
        JColorChooser cc=new JColorChooser();
        Color c=cc.showDialog(this,"颜色设置",Color.black);
        return  c;
//        return  null;
    }

    private void save() throws IOException {
        JFileChooser fc=new JFileChooser();//对文件进行选择的对话框
        if (fc.showSaveDialog(this)==0){
            File file=fc.getSelectedFile();
            FileWriter fw=new FileWriter(file);
                BufferedWriter bw=new BufferedWriter(fw);
                String str =textArea.getText();
                str.replace("\12","\12\45");
                bw.write(textArea.getText().toString());
                bw.write(str);
                fw.close();
                fw.close();



        }

//        int n=fc.showOpenDialog(this);
//        if(n==JFileChooser.SAVE_DIALOG){
//            File file = fc.getSelectedFile();//得到文件对象
//            System.out.println(file.getName());
//            FileWriter fr=new FileWriter(file);
//            BufferedWriter br=new BufferedWriter(fr);
//            StringBuffer sb=new StringBuffer();
//            String[] str=txtcontents.getText()
//        }

    }

    //打开方法
    private void open() throws IOException {
        JFileChooser fc=new JFileChooser("d:\\");//对文件进行选择的对话框

        //只有txt文件 ；
        fc.setFileFilter(new FileFilter() {
//            FileFilter --过滤器

            @Override
            public boolean accept(File f) {
                //接收文件 ；
//                return false;
                return  f.getName().endsWith(".txt");
            }


            @Override
            public String getDescription() {
                //getDescription 文件后缀
//                return null;
                return  ".txt";
            }
        });

        int n=fc.showOpenDialog(this);
//        System.out.println(n+"\n");
        if (n==0){
            setTitle(fc.getSelectedFile().getName());
            File file = fc.getSelectedFile();//得到文件对象
            FileReader fr=new FileReader(file);//字符流
            BufferedReader br=new BufferedReader(fr);//缓冲流
            StringBuffer sb=new StringBuffer();//变长
            int n1=0;
            char[] cs=new char[1024];
            while ((n1=br.read(cs))!=-1){
                sb.append(new String(cs,0,n1));
            }
            textArea.setText(sb.toString());
            br.close();
            br.close();
        }
    }
}
